package com.fiberhome.app.network.http;

import android.text.TextUtils;

import com.fiberhome.app.network.CacheBuilder;
import com.fiberhome.app.network.IBuilder;
import com.fiberhome.app.network.ISender;
import com.fiberhome.app.network.MD5Util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Deed on 2018/1/18.
 */

public class HttpBuilder extends CacheBuilder {

    public static final int LIB_OKHTTP = 0;
//    public static final int LIB_VOLLEY = 1;

    public static final int GET = 0;
    public static final int POST = 1;

    String url;
    int method;
    Map<String,String> params;
    Map<String,String> headers;

    boolean https;

    private String string;
    private int useLib = LIB_OKHTTP;

    /**
     *
     * @param https boolean
     */
    HttpBuilder(boolean https) {
        this.https = https;
    }

    @Override
    public ISender build() {
//        if (useLib == LIB_VOLLEY) {
//            return new VolleySender(this);
//        }
        return new OKHttpSender(this);
    }

//    /**
//     *  LIB_OKHTTP,
//     *  LIB_VOLLEY;
//     * @param libType LIB_OKHTTP, LIB_VOLLEY;
//     * @return HttpBuilder
//     */
//    public HttpBuilder useLib(int libType) {
//        this.useLib = libType;
//        return this;
//    }

    /**
     *
     * @param cache boolean
     * @return HttpBuilder
     */
    public HttpBuilder useCache(int cache) {
        super.useCache(cache);
        return this;
    }

    /**
     * @return HttpBuilder
     */
    public HttpBuilder cacheOverdue(long cacheOverdue) {
        super.cacheOverdue(cacheOverdue);
        return this;
    }

    /**
     *
     * @param key String
     * @param value String
     * @return HttpBuilder
     */
    public HttpBuilder addHeader(String key,String value) {
        if (headers == null) {
            headers = new HashMap<String, String>();
        }
        headers.put(key,value);
        return this;
    }

    /**
     *
     * @param headers (Map<String,String>
     * @return HttpBuilder
     */
    public HttpBuilder setHeaders(Map<String,String> headers) {
        this.headers = headers;
        return this;
    }

    /**
     *
     * @param key String
     * @param value String
     * @return HttpBuilder
     */
    public HttpBuilder addParam(String key,String value) {
        if (params == null) {
            params = new HashMap<String, String>();
        }
        params.put(key,value);
        return this;
    }

    /**
     *
     * @param params Map<String ,String>
     * @return HttpBuilder
     */
    public HttpBuilder setParams(Map<String ,String> params) {
        this.params = params;
        return this;
    }

    @Override
    public String string() {
        if (TextUtils.isEmpty(string)) {
            StringBuilder sb = new StringBuilder(url);
            sb.append(method);
            if (headers != null && headers.size() > 0) {
                for (Map.Entry entry : headers.entrySet()) {
                    sb.append(entry.getKey() + ":" + entry.getValue());
                }
            }
            if (params != null && params.size() > 0) {
                for (Map.Entry entry : params.entrySet()) {
                    sb.append(entry.getKey() + ":" + entry.getValue());
                }
            }
            string = MD5Util.md5(sb.toString());
        }
        return string;
    }
}
